﻿using System.Collections.Generic;
using System;
namespace Tfs.OnTheRoad.ViewModel
{
    /// <summary>
    /// Class used to store the Work Item charring unit
    /// </summary>
    public class WorkItemSummaryItem
    {
        public WorkItemSummaryItem()
        {
            Items = new List<ChildSummaryItem>();
        }

        public string PrepareField(string fieldName, object fieldValue)
        {
            //http://social.msdn.microsoft.com/Forums/pl-PL/adodotnetdataservices/thread/646ca69a-cd3d-4713-8fa6-e857c8bac17c
            if (fieldValue is DateTime)
                return string.Format("({0} eq datetime'{1}')", fieldName, ((DateTime)fieldValue).ToString("yyyy-MM-dd"));                
            else
                return string.Format("({0} eq '{1}')", fieldName, fieldValue); ;
        }
        public string Filter
        {
            get
            {
                string toReturn = PrepareField(FieldName, GroupLevel1);
                if(!string.IsNullOrEmpty(PreviousLevelFilter))
                    toReturn = string.Format("{0} and ({1})", toReturn, PreviousLevelFilter);
                return toReturn;
            }
        }
        private string _caption = null;
        public string Caption
        {
            get
            {
                if(_caption == null)
                    _caption =string.Format("{0}:{1} ", GroupLevel1,Count);
                return _caption;
            }
            set
            {
                _caption = value;
            }
        }
        /// <summary>
        /// Holds the filter information from the previous level in a way that the filter is being hold for next drill down operations
        /// </summary>
        public string PreviousLevelFilter { get; set; }
        /// <summary>
        /// Holds the currentLevel number, which will be used to save Chart settings for each level, allowing the user to save different charts on different levels
        /// </summary>
        public int CurrentLevel { get; set; }
        public string FieldName { get; set; }
        public object GroupLevel1 { get; set; }
        public int Count { get; set; }
        public List<ChildSummaryItem> Items { get; set; }
        public class ChildSummaryItem
        {
            public string FieldName { get; set; }
            public object ChildLevel { get; set; }
            private string _caption = null;
            public string Caption
            {
                get
                {
                    if(_caption == null)
                        _caption = string.Format("{0}:{1} ", ChildLevel, Count);
                    return _caption;
                }
                set
                {
                    _caption = value;
                }
            }
            public string Filter
            {
                get
                {
                    return string.Format("{0} and {1}", Parent.Filter, Parent.PrepareField(FieldName, ChildLevel));
                }
            }
            public int Count { get; set; }
            public WorkItemSummaryItem Parent { get; set; }
        }
    }

    /// <summary>
    /// List of Work Items Summaries, used to show on the stacked chart
    /// </summary>
    public class WorkItemsSummaryList : System.Collections.ObjectModel.ObservableCollection<WorkItemSummaryItem>
    {
    }
}
